/**   
 * @Title: SportteryBasketballChooseList.java 
 * @Package com.esun.rabbit2.viewer.mainact.lotteryhallact.sportterybasketballapg 
 * @Description: (竞彩足球购彩页面) 
 * @author: liugan  
 * @date: 2012-3-13 下午14;45
 * Copyright: (C) 2011 Shenzhen E-sun Sky Net Technology Co.,ltd.
 */
package com.esun.rabbit2.viewer.mainact.lotteryhallact.sportterybasketballapg;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Hashtable;

import com.esun.config.Config;
import com.esun.config.Language;
import com.esun.libc.ESInterface;
import com.esun.libc.TouZhuData;
import com.esun.model.UserInfo;
import com.esun.model.sportterybasketball.SportteryBasketballAgainstInfo;
import com.esun.model.sportterybasketball.SportteryBasketballLotteryInfo;
import com.esun.rabbit2.communicaters.sportterybasketball.SportteryBasketballNormalBuyCommunicater;
import com.esun.rabbit2.viewer.LoginAct;
import com.esun.rabbit2.viewer.mainact.lotteryhallact.SportteryBasketballAgp;
import com.esun.rabbit2.viewer.mainact.lotteryhallact.SportteryBuyLottery;
import com.esun.rabbit2.viewer.mainact.lotteryhallact.SportteryChooseList;
import com.esun.rabbit2.viewer.mainact.mylotteryact.recharg.RechargeMenuPage;
import com.esun.ui.R;
import com.esun.util.LoginTimeControl;
import com.esun.util.ViewerUtil;
import com.esun.util.custom.GridViewNoScroll;
import com.esun.util.log.EDebug;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.Html;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout.LayoutParams;
import com.esun.rabbit2.viewer.mainact.lotteryhallact.SportteryChooseActivity;

/**
 * @className: SportteryBasketballBuyLottery
 * @description: 竞彩篮球购彩界面
 * @author liugan
 * @date: 2012-3-13 下午14;45
 */
public class SportteryBasketballBuyLottery extends SportteryBuyLottery implements OnClickListener {

	private int zhushu;// 组数
	private int beishu;// 倍数
	private int guoguanType; // 过关类型
	private String codes; // 请求参数
	private String sgtype;
	private String strGuoguan; // 过关的字符串
	private Context context;
	private View view; // 购彩界面的View
	private LayoutInflater inflater;

	private SportteryChooseList chooseListPage; // 比赛列表界面

	private Button btnBack;
	private Button btnGuoguan;
	private Button btnClean;
	private Button btnHemai;
	private Button btnNormalBuy;

	private TextView tvGuoguan;
	private TextView tvChangCount;
	private TextView tvBetCount;
	private TextView tvMoney;
	private TextView tvJackpot;
	private TextView tvJiezhiTime;
	// private TextView tvBalance;
	private EditText edtBeitou;

	private Dialog dialogGuoguan; // 过关方式选择对话框
	private Dialog dialogBuySucceed;// 购买成功后的对话框
	private Dialog dialogBalanceNotEnough; // 剩余金额不足时的对话框
	private GridView gridViewDuochuan; // 多串过关方式选择 的视图
	private GridView gridViewZiyou;// 自由过关方式选择 的视图
	private RadioButton rbZiyou; // 自由过关方式选择 的RadioButton
	private DuochuanListAdapter duochuanListAdapter; // 多串过关的适配器

	private ArrayList<CheckBox> listCbxDan = new ArrayList<CheckBox>(); // 胆码选项

	private ArrayList<Hashtable<String, Object>> listSGType = new ArrayList<Hashtable<String, Object>>();
	private ArrayList<Hashtable<String, Object>> listZiyou = new ArrayList<Hashtable<String, Object>>(); // 可以选择的自由过关方式
	private ArrayList<Hashtable<String, Object>> listDuochuan = new ArrayList<Hashtable<String, Object>>();// 可以选择的多串过关方式
	private ArrayList<ArrayList<CheckBox>> danList = new ArrayList<ArrayList<CheckBox>>(); // 胆码

	private int limitDanForDialogGuoguan;

	private LinearLayout linearList;

	private SportteryBasketballNormalBuyCommunicater sportteryBasketballNormalBuyCommunicater;
	private ProgressDialog progressdialogNormalBuy;
	// 多串过关上次选中的按钮index
	private int lastItem = -1;

	/**
	 * @title：SportteryBasketballBuyLottery
	 * @description：构造方法
	 * @param context
	 *            父Activity
	 * @param view
	 *            当前界面的视图
	 */
	public SportteryBasketballBuyLottery(Context context, View view) {
		this.view = view;
		this.context = context;
		inflater = ((Activity) context).getLayoutInflater();

		init();
		handlerInit();
		updateListView();
	}

	/**
	 * @title:dataInit
	 * @description: 相关数据初始化
	 * 
	 */
	private void dataInit() {
		zhushu = 0;
		beishu = 1;
		guoguanType = 0;
		codes = Config.NULLMARK;
		sgtype = Config.NULLMARK;
		listSGType.clear();
		listZiyou.clear();
		listDuochuan.clear();
		strGuoguan = "过关方式";
		updateGuoguanTypeList();
		listSGType.addAll(listZiyou);
		if (!listSGType.isEmpty()) {
			guoguanType = 3;
			sgtype = Integer.toString((Integer) listSGType.get(listSGType.size() - 1).get(TAG));
			listSGType.get(listSGType.size() - 1).put(IS_CHECKED, true);
			strGuoguan = (String) listSGType.get(listSGType.size() - 1).get(STR_TYPE);
		}
		for (int i = 0; i < listCbxDan.size(); i++) {
			listCbxDan.get(i).setEnabled(false);
		}
		setLimitDanNum(7);
		updateTextViewSet();
	}

	/**
	 * @title:setListCbxDan
	 * @description:设置胆码按钮状态
	 * @param b
	 */
	private void setListCbxDan(boolean b) {
		for (int i = 0; i < listCbxDan.size(); i++) {
			listCbxDan.get(i).setEnabled(b);
		}
	};

	/** 过关方式在删除一条的时候修改操作 */
	private void updateGuoGuanType() {

		if (guoguanType == 3) {
			// 自由过关
			for (int i = listSGType.size() - 1; i > 0; i--) {
				if ((Boolean) listSGType.get(i).get(IS_CHECKED)) {
					EDebug.o(SportteryBasketballBuyLottery.this, "act is " + getActiveItemNum() + "& team count is " + (Integer) listSGType.get(i).get(TEAM_COUNT) + "++++++++++++++++");
					if (getActiveItemNum() < ((Integer) listSGType.get(i).get(TEAM_COUNT))) {
						listSGType.get(i).put(IS_CHECKED, false);
						EDebug.o(SportteryBasketballBuyLottery.this, "i is " + i + "& activ num is " + getActiveItemNum() + "+++++++++++++++++");
						break;
					} else if (getActiveItemNum() == ((Integer) listSGType.get(i).get(TEAM_COUNT))) {
						cleanDanItem();
						setListCbxDan(false);
					} else {
						setListCbxDan(true);
						setLimitDanNum((Integer) listSGType.get(i).get(TEAM_COUNT) - 1);
					}
				}
			}
			StringBuffer sb = new StringBuffer();
			StringBuffer sbSgType = new StringBuffer();
			for (int i = 0; i < listSGType.size(); i++) {
				if ((Boolean) listSGType.get(i).get(IS_CHECKED)) {
					sb.append((String) listSGType.get(i).get(STR_TYPE)).append(Config.ONESPACE);
					sbSgType.append(Integer.toString((Integer) listSGType.get(i).get(TAG))).append(Config.COMMA);
				}
			}
			strGuoguan = sb.toString();
			if (strGuoguan.length() == 0) {
				strGuoguan = "请选择过关方式";
				setListCbxDan(true);
				setLimitDanNum(7);
				sgtype = Config.NULLMARK;
				guoguanType = 0;
			} else {
				sgtype = sbSgType.toString();
				sgtype = sgtype.substring(0, sgtype.length() - 1);
			}
			EDebug.o(SportteryBasketballBuyLottery.this, "size is " + sgtype.length() + "&& valve is " + sgtype + "++++++++++++++++++++++++++++++");
			updateTextViewSet();
		} else if (guoguanType == 2) {
			// 多串过关
			int maxTeamCount = 0;
			for (int i = listSGType.size() - 1; i > 0; i--) {
				if ((Boolean) listSGType.get(i).get(IS_CHECKED)) {
					maxTeamCount = (Integer) listSGType.get(i).get(TEAM_COUNT);
					break;
				}
			}
			if (getActiveItemNum() < maxTeamCount) {
				listSGType.clear();
				updateGuoguanTypeList();
				listSGType.addAll(listZiyou);
				guoguanType = 0;
				sgtype = Config.NULLMARK;
				strGuoguan = "请选择过关方式";
				setListCbxDan(true);
				setLimitDanNum(7);
				updateTextViewSet();
			} else if (getActiveItemNum() == maxTeamCount) {
				cleanDanItem();
				setListCbxDan(false);
				updateTextViewSet();
			} else {
				setListCbxDan(true);
				setLimitDanNum(maxTeamCount - 1);
			}
		}
	}

	/** 更新主列表 */
	private void updateListView() {
		linearList.removeAllViews();
		listCbxDan.clear();
		danList.clear();
		LayoutParams param = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		for (int i = 0; i < SportteryBasketballLotteryInfo.getSportteryBasketballLotteryInfo().getLotterList().size(); i++) {
			final int i_ = i;
			if ((Boolean) SportteryBasketballLotteryInfo.getSportteryBasketballLotteryInfo().getLotterList().get(i).get(IS_ACTIVE)) {
				EDebug.o(SportteryBasketballBuyLottery.this, " 走你");
				SportteryBasketballAgainstInfo sportteryBasketballAgainstInfo = (SportteryBasketballAgainstInfo) SportteryBasketballLotteryInfo.getSportteryBasketballLotteryInfo().getLotterList()
						.get(i).get(INFO);
				View listItem = inflater.inflate(R.layout.sportterybasketball_choose_act_list_item, null);
				listItem.setTag(i);
				((TextView) listItem.findViewById(R.id.sportterybasketballchooseactlistitem_tv_homeName)).setText(strLengthOption((String) sportteryBasketballAgainstInfo.getHomeSxName(), 3));
				((TextView) listItem.findViewById(R.id.sportterybasketballchooseactlistitem_tv_visiterName)).setText(strLengthOption((String) sportteryBasketballAgainstInfo.getAwaySxName(), 3));
				final CheckBox cbxW = (CheckBox) listItem.findViewById(R.id.sportterybasketballchooseactlistitem_cbx_w);
				final CheckBox cbxL = (CheckBox) listItem.findViewById(R.id.sportterybasketballchooseactlistitem_cbx_l);
				final CheckBox cbxDan = (CheckBox) listItem.findViewById(R.id.sportterybasketballchooseactlistitem_cbx_dan);
				final TextView tvWPL = (TextView) listItem.findViewById(R.id.sportterybasketballchooseactlistitem_tv_wpl);
				final TextView tvLPL = (TextView) listItem.findViewById(R.id.sportterybasketballchooseactlistitem_tv_lpl);
				final TextView tvW = (TextView) listItem.findViewById(R.id.sportterybasketballchooseactlistitem_tv_w);
				final TextView tvL = (TextView) listItem.findViewById(R.id.sportterybasketballchooseactlistitem_tv_l);
				cbxW.setChecked((Boolean) SportteryBasketballLotteryInfo.getSportteryBasketballLotteryInfo().getLotterList().get(i).get(W));
				if (cbxW.isChecked()) {
					tvW.setTextColor(TEXT_CHECKED_COLOR);
					tvWPL.setTextColor(TEXT_CHECKED_COLOR);
				} else {
					tvW.setTextColor(TEXT_UNCHECKED_COLOR);
					tvWPL.setTextColor(TEXT_UNCHECKED_COLOR);
				}

				cbxL.setChecked((Boolean) SportteryBasketballLotteryInfo.getSportteryBasketballLotteryInfo().getLotterList().get(i).get(L));
				if (cbxL.isChecked()) {
					tvL.setTextColor(TEXT_CHECKED_COLOR);
					tvLPL.setTextColor(TEXT_CHECKED_COLOR);
				} else {
					tvL.setTextColor(TEXT_UNCHECKED_COLOR);
					tvLPL.setTextColor(TEXT_UNCHECKED_COLOR);
				}
				SportteryBasketballAgainstInfo info = (SportteryBasketballAgainstInfo) SportteryBasketballLotteryInfo.getSportteryBasketballLotteryInfo().getLotterList().get(i).get(INFO);
				tvWPL.setText(info.getWin());
				tvLPL.setText(info.getLost());
				// add by billy
				ArrayList<CheckBox> checkBoxTemp = new ArrayList<CheckBox>();
				checkBoxTemp.add(cbxW);
				checkBoxTemp.add(cbxL);
				checkBoxTemp.add(cbxDan);

				danList.add(checkBoxTemp);

				listCbxDan.add(cbxDan);

				Button btnDelete = (Button) listItem.findViewById(R.id.sportterybasketballchooseactlistitem_btn_delete);
				btnDelete.setOnClickListener(this);
				OnCheckedChangeListener onCheckedChangeListener = new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
						switch (buttonView.getId()) {
						case R.id.sportterybasketballchooseactlistitem_cbx_w:
							if (isChecked) {
								tvW.setTextColor(TEXT_CHECKED_COLOR);
								tvWPL.setTextColor(TEXT_CHECKED_COLOR);
							} else {
								tvW.setTextColor(TEXT_UNCHECKED_COLOR);
								tvWPL.setTextColor(TEXT_UNCHECKED_COLOR);
							}
							// if (!cbxW.isChecked() && !cbxD.isChecked() &&
							// !cbxL.isChecked()) {
							// cbxDan.setChecked(false);
							// cbxDan.setEnabled(false);
							// updateDanMaState();
							// EDebug.o(SportteryBasketballChooseList.this,getDanItemNum()+"===========================");
							// } else {
							// cbxDan.setEnabled(true);
							// }
							updateLotteryListItem(i_, isChecked, cbxL.isChecked(), cbxDan);
							// updateDanMaState(i_);
							break;

						// case R.id.sportterybasketballchooseactlistitem_cbx_d:
						// if(isChecked){
						// tvD.setTextColor(TEXT_CHECKED_COLOR);
						// tvDPL.setTextColor(TEXT_CHECKED_COLOR);
						// }else{
						// tvD.setTextColor(TEXT_UNCHECKED_COLOR);
						// tvDPL.setTextColor(TEXT_UNCHECKED_COLOR);
						// }
						// if (!cbxW.isChecked() && !cbxD.isChecked() &&
						// !cbxL.isChecked()) {
						// cbxDan.setEnabled(false);
						// cbxDan.setChecked(false);
						// updateDanMaState();
						// EDebug.o(SportteryBasketballChooseList.this,getDanItemNum()+"===========================");
						// } else {
						// cbxDan.setEnabled(true);
						// // }
						// updateLotteryListItem(i_, cbxW.isChecked(), cbxL.isChecked(), cbxDan);
						// // updateDanMaState(i_);
						// break;

						case R.id.sportterybasketballchooseactlistitem_cbx_l:
							if (isChecked) {
								tvL.setTextColor(TEXT_CHECKED_COLOR);
								tvLPL.setTextColor(TEXT_CHECKED_COLOR);
							} else {
								tvL.setTextColor(TEXT_UNCHECKED_COLOR);
								tvLPL.setTextColor(TEXT_UNCHECKED_COLOR);
							}
							// if (!cbxW.isChecked() && !cbxD.isChecked() &&
							// !cbxL.isChecked()) {
							// cbxDan.setEnabled(false);
							// cbxDan.setChecked(false);
							// updateDanMaState();
							// EDebug.o(SportteryBasketballChooseList.this,getDanItemNum()+"===========================");
							// } else {
							// cbxDan.setEnabled(true);
							// }
							updateLotteryListItem(i_, cbxW.isChecked(), isChecked, cbxDan);
							// updateDanMaState(i_);
							break;

						case R.id.sportterybasketballchooseactlistitem_cbx_dan:
							EDebug.o(SportteryBasketballBuyLottery.this, strGuoguan.substring(0, 1));
							if (isChecked) {
								updateTextViewSet();
								EDebug.o(SportteryBasketballBuyLottery.this, "limit dan num is " + getLimitDanNum());
								// SportteryBasketballLotteryInfo.getSportteryBasketballLotteryInfo().getLotterList().get(i_).put(IS_DAN,
								// true);
								if (!cbxW.isChecked() && !cbxL.isChecked()) {
									cbxDan.setChecked(false);
									// cbxDan.setEnabled(false);
								} else if (getDanItemNum() > LIMIT_DAN_COUNT - 1 || getDanItemNum() > getActiveItemNum() - 2 || getDanItemNum() > getLimitDanNum() - 1) {
									cbxDan.setChecked(false);
									// cbxDan.setEnabled(false);
									EDebug.o(SportteryBasketballBuyLottery.this, "limit dan num is " + getLimitDanNum());
								} else {
									EDebug.o(SportteryBasketballBuyLottery.this, "limit dan num is " + getLimitDanNum());
									SportteryBasketballLotteryInfo.getSportteryBasketballLotteryInfo().getLotterList().get(i_).put(IS_DAN, true);
									if (getDanItemNum() == LIMIT_DAN_COUNT || getDanItemNum() == getActiveItemNum() - 1 || getDanItemNum() == getLimitDanNum()) {
										updateDanList(false);
									}
									updateTextViewSet();
								}
							} else {
								updateDanList(true);
								SportteryBasketballLotteryInfo.getSportteryBasketballLotteryInfo().getLotterList().get(i_).put(IS_DAN, false);
								updateTextViewSet();
							}
							// updateDanMaState(i_);
							EDebug.o(SportteryBasketballBuyLottery.this, "dan num is " + getDanItemNum());
							break;

						default:
							break;
						}
					}
				};
				cbxW.setOnCheckedChangeListener(onCheckedChangeListener);
				cbxL.setOnCheckedChangeListener(onCheckedChangeListener);
				cbxDan.setOnCheckedChangeListener(onCheckedChangeListener);
				linearList.addView(listItem, param);
			}
		}
	}

	/**
	 * @title updateDanList
	 * @description
	 * @param b
	 *            需要设置的状态
	 */
	private void updateDanList(boolean b) {
		for (int i = 0; i < listCbxDan.size(); i++) {
			if (!listCbxDan.get(i).isChecked()) {
				listCbxDan.get(i).setEnabled(b);
			}
		}
	}

	/**
	 * @title:handlerInit
	 * @description: 初始化Handler并传给父类
	 */
	private void handlerInit() {

		Handler handler = new Handler() {
			public void handleMessage(Message msg) {
				progressdialogNormalBuy.dismiss();
				switch (msg.what) {
				case BUY_SUCCEED:

					buySucceed();
					break;

				case BUY_FAIL:
					if (sportteryBasketballNormalBuyCommunicater.getState() == 0) {
						ViewerUtil.showTipDialog(context, sportteryBasketballNormalBuyCommunicater.getErrorMSG() + Config.COMMA + Language.POPLOTTERYTIP_LOOKOVERRECORD, false);
					} else if (sportteryBasketballNormalBuyCommunicater.getState() == 105) {
						popBalanceNotEnoughAlert();
					} else {
						// ViewerUtil.showTipDialog(context, sportteryBasketballNormalBuyCommunicater.getErrorMSG(), false);
						Toast.makeText(context, sportteryBasketballNormalBuyCommunicater.getErrorMSG(), 0).show();
					}

					break;

				case Config.CONNECTION_FAIL:
					ViewerUtil.showTipDialog(context, Language.STR_CONNECTIONFAIL + Config.COMMA + Language.POPLOTTERYTIP_LOOKOVERRECORD, false);
					break;

				default:
					break;
				}
			}
		};
		super.setHandler(handler);

	}

	/**
	 * @title: init
	 * @description:初始化界面控件
	 */
	private void init() {
		btnBack = (Button) view.findViewById(R.id.sportterybasketballchooseactlist_btn_back);
		btnGuoguan = (Button) view.findViewById(R.id.sportterybasketballchooseactlist_btn_guoguan);
		btnGuoguan.setText("自由过关");
		btnClean = (Button) view.findViewById(R.id.sportterybasketballchooseactlist_btn_clean);
		btnHemai = (Button) view.findViewById(R.id.sportterybasketballchooseactlist_btn_hemai);
		btnNormalBuy = (Button) view.findViewById(R.id.sportterybasketballchooseactlist_btn_normalBy);

		btnBack.setOnClickListener(this);
		btnGuoguan.setOnClickListener(this);
		btnClean.setOnClickListener(this);
		btnHemai.setOnClickListener(this);
		btnNormalBuy.setOnClickListener(this);

		tvGuoguan = (TextView) view.findViewById(R.id.sportterybasketballchooseactlist_tv_guoguan);
		tvChangCount = (TextView) view.findViewById(R.id.sportterybasketballchooseactlist_tv_changCount);
		tvBetCount = (TextView) view.findViewById(R.id.sportterybasketballchooseactlist_tv_betCount);
		tvMoney = (TextView) view.findViewById(R.id.sportterybasketballchooseactlist_tv_money);
		tvJackpot = (TextView) view.findViewById(R.id.sportterybasketballchooseactlist_tv_jackpot);
		tvJiezhiTime = (TextView) view.findViewById(R.id.sportterybasketballchooseactlist_tv_jiezhiTime);
		// tvBalance = (TextView) view.findViewById(R.id.sportterybasketballchooseactlist_tv_balance);
		edtBeitou = (EditText) view.findViewById(R.id.sportterybasketballchooseactlist_edt_beitou);

		linearList = (LinearLayout) view.findViewById(R.id.sportterybasketballchooseactlist_linear_list);
		setLimitDanNum(LIMIT_DAN_COUNT);

		sportteryBasketballNormalBuyCommunicater = new SportteryBasketballNormalBuyCommunicater();

		edtBeitou.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				if (!(String.valueOf(s).equals(Config.NULLMARK)) && s != null) {

					Toast partCountToast = Toast.makeText(context, Language.STR_QINGSHURU + Config.NUM1 + Config.HORIZONTAL_LINE + "100000" + Language.STR_ZHIJIANDESHUZI, Toast.LENGTH_SHORT);
					beishu = Integer.parseInt(String.valueOf(s));
					if (beishu > 100000) {
						partCountToast.show();
						beishu = 100000;
						edtBeitou.setText(Integer.toString(beishu));
					} else if (beishu < 1) {
						partCountToast.show();
						beishu = 1;
						edtBeitou.setText(Integer.toString(beishu));
					} else {

					}
				} else {
					beishu = 0;
				}
				updateTextViewSet();
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub

			}
		});
		progressdialogNormalBuy = ViewerUtil.showProgressDialog(context, Language.POPLOTTERYTIP_BUYING);

	}

	/**
	 * @title:threadNormalBuy
	 * @description: 执行竞彩篮球复式代购
	 */
	private void threadNormalBuy() {
		new Thread() {
			@Override
			public void run() {
				super.run();
				try {
					boolean b = sportteryBasketballNormalBuyCommunicater.isBuySucceed(zhushu, beishu, ((long) beishu * (long) zhushu * 2), guoguanType, codes, sgtype);
					if (b) {
						Message msg = handlerChooseList.obtainMessage(BUY_SUCCEED, null);
						handlerChooseList.sendMessage(msg);
					} else {
						Message msg = handlerChooseList.obtainMessage(BUY_FAIL, null);
						handlerChooseList.sendMessage(msg);
					}
				} catch (Exception e) {
					Message msg = handlerChooseList.obtainMessage(Config.CONNECTION_FAIL, null);
					handlerChooseList.sendMessage(msg);
				}
			}
		}.start();
	}

	/**
	 * @title:getView
	 * @description:获得竞彩篮球购彩的View（先更新界面控件）
	 */
	public View getView() {
		((SportteryChooseActivity) context).setCurrentPageFlag(1);
		updateListView();
		dataInit();
		updateTextViewSet();
		edtBeitou.setText(Integer.toString(beishu));
		Message msg = SportteryBasketballAgp.handlerSportteryBasketballAgp.obtainMessage(SportteryBasketballAgp.IN_LIST_VIEW, null);
		SportteryBasketballAgp.handlerSportteryBasketballAgp.sendMessage(msg);
		return view;
	}

	@Override
	public void onClick(View v) {
		Message msg = SportteryBasketballChooseList.handlerChoosePage.obtainMessage(SportteryBasketballChooseList.DO_BACK_UPDATE, null);
		switch (v.getId()) {
		case R.id.sportterybasketballchooseactlist_btn_back:// 后退
			SportteryBasketballChooseList.handlerChoosePage.sendMessage(msg);
			((Activity) context).setContentView(chooseListPage.getView());
			break;

		case R.id.sportterybasketballchooseactlist_btn_clean:// 清除所有选择的比赛
			cleanTheList();
			SportteryBasketballChooseList.handlerChoosePage.sendMessage(msg);
			((Activity) context).setContentView(chooseListPage.getView());
			break;

		case R.id.sportterybasketballchooseactlist_btn_guoguan:// 选择过关方式
			updateGuoguanTypeList();
			showDialogGuoguan();
			break;

		case R.id.sportterybasketballchooseactlist_btn_hemai: // 发起合买
			checkHemai();
			break;

		case R.id.sportterybasketballchooseactlist_btn_normalBy:// 复式代购
			checkNomalBuy();
			break;

		case R.id.sportterybasketballchooseactlistitem_btn_delete: // 删除对阵
			linearList.removeView((View) v.getParent());
			deleteLotteryListItem((Integer) ((View) v.getParent()).getTag());
			updateTextViewSet();
			if (SportteryBasketballLotteryInfo.getSportteryBasketballLotteryInfo().getActiveTeamCount() < 1) {
				SportteryBasketballChooseList.handlerChoosePage.sendMessage(msg);
				((Activity) context).setContentView(chooseListPage.getView());
			} else if (getActiveItemNum() < getDanItemNum()) {
				strGuoguan = Config.NULLMARK;
				listSGType.clear();
				cleanDanItem();
				// list
			}
			updateGuoGuanType();
			break;

		default:
			break;
		}

	}

	/**
	 * @title checkHemai
	 * @description: 检查能否合买，并跳转
	 */
	private void checkHemai() {
		updateNormalBy();
		SportteryBasketballLotteryInfo.getSportteryBasketballLotteryInfo().setCodes(codes);
		SportteryBasketballLotteryInfo.getSportteryBasketballLotteryInfo().setGuoguanType(guoguanType);
		SportteryBasketballLotteryInfo.getSportteryBasketballLotteryInfo().setMultiple(beishu);
		SportteryBasketballLotteryInfo.getSportteryBasketballLotteryInfo().setSgType(sgtype);
		SportteryBasketballLotteryInfo.getSportteryBasketballLotteryInfo().setZhushuCount(zhushu);
		Intent intentToHemai = new Intent();
		intentToHemai.setClass(context, SportteryBasketballFaqiHemaiAct.class);
		if (beishu > 0 && zhushu > 0) {
			if (UserInfo.getUserInfo().getUserSession().length() < 3) {
				Intent intent = new Intent();
				intent.setClass(context, LoginAct.class);
				((Activity) context).startActivity(intent);
			} else {
				((Activity) context).startActivity(intentToHemai);
			}
		} else {
			ViewerUtil.showTipDialog(context, "请正确填写信息", false);
		}
	}

	/**
	 * @title checkNomalBuy
	 * @description 检查复式代购，登陆超时跳转到登陆界面，正常则发起购买
	 */
	private void checkNomalBuy() {

		if (updateNormalBy()) {
			if (beishu > 0 && zhushu > 0) {
				if (UserInfo.getUserInfo().getUserSession().length() < 3) {
					Intent intent = new Intent();
					intent.setClass(context, LoginAct.class);
					((Activity) context).startActivity(intent);
				} else {
					LoginTimeControl test = new LoginTimeControl();
					if (test.isNeedInputPwd()) {
						test.showInputPasswordDialog(context);
					} else {
						progressdialogNormalBuy.show();
						threadNormalBuy();
					}
				}
			} else if (!(zhushu > 0)) {
				ViewerUtil.showTipDialog(((Activity) context), "请选择过关方式", false);
			} else {
				ViewerUtil.showTipDialog(((Activity) context), "请正确填写信息", false);
			}
		}
	}

	/**
	 * @title：updateNormalBy
	 * @description：更新购买信息 codes参数说明 48096|3004[0#3.80,1#1.22]*0$48100|3005[3#2.82]*1：
	 * 
	 *                     48096: 对阵ID 3004：对阵编号名称 2：负 1：胜 3.80：赔率 0：非胆码 1：胆码
	 */

	private boolean updateNormalBy() {

		if (this.getActiveItemNum() < 2) {
			ViewerUtil.showTipDialog(((Activity) context), "请选择至少两个队伍", false);
			return false;
		}

		StringBuffer sbCodes = new StringBuffer();

		int chosenSize = SportteryBasketballLotteryInfo.getSportteryBasketballLotteryInfo().getLotterList().size();

		Hashtable<String, Object> oneTeam = new Hashtable<String, Object>();
		for (int i = 0; i < chosenSize; i++) {
			oneTeam = SportteryBasketballLotteryInfo.getSportteryBasketballLotteryInfo().getLotterList().get(i);
			if ((Boolean) oneTeam.get(IS_ACTIVE)) {
				SportteryBasketballAgainstInfo sportteryBasketballAgainstInfo = (SportteryBasketballAgainstInfo) oneTeam.get(INFO);
				sbCodes.append(sportteryBasketballAgainstInfo.getxId()).append(Config.VERTRAL_LINE).append(sportteryBasketballAgainstInfo.getProcessName()).append(Config.LEFT_BR);
				if ((Boolean) oneTeam.get(W)) {
					sbCodes.append("1").append(Config.JINGHAO).append(sportteryBasketballAgainstInfo.getWin()).append(Config.COMMA);
				}
				if ((Boolean) oneTeam.get(L)) {
					sbCodes.append("2").append(Config.JINGHAO).append(sportteryBasketballAgainstInfo.getLost()).append(Config.COMMA);
				}
				sbCodes.deleteCharAt(sbCodes.length() - 1);
				sbCodes.append(Config.RIGHT_BR).append(Config.XINGHAO);
				if ((Boolean) oneTeam.get(IS_DAN)) {
					sbCodes.append(Config.NUM1);
				} else {
					sbCodes.append(Config.NUM0);
				}
				sbCodes.append(Config.DOLLAR);
			}

		}
		sbCodes.deleteCharAt(sbCodes.length() - 1);
		codes = sbCodes.toString();
		EDebug.o(SportteryBasketballBuyLottery.this, "the codes is " + codes);
		zhushu = getZhushuCount(SportteryBasketballLotteryInfo.getSportteryBasketballLotteryInfo().getActiveTeamCount(), SportteryBasketballLotteryInfo.getSportteryBasketballLotteryInfo()
				.getLotterList(), listSGType);
		EDebug.o(SportteryBasketballBuyLottery.this, "the betCount is " + zhushu);
		return true;
	}

	/**
	 * @title：updateTextViewSet
	 * @description：更新textview显示
	 */
	private void updateTextViewSet() {
		tvGuoguan.setText(strGuoguan);
		tvChangCount.setText(Integer.toString((Integer) SportteryBasketballLotteryInfo.getSportteryBasketballLotteryInfo().getActiveTeamCount()));
		zhushu = getZhushuCount(SportteryBasketballLotteryInfo.getSportteryBasketballLotteryInfo().getActiveTeamCount(), SportteryBasketballLotteryInfo.getSportteryBasketballLotteryInfo()
				.getLotterList(), listSGType);
		tvBetCount.setText(Integer.toString(zhushu));
		DecimalFormat f = new DecimalFormat("0.00");
		tvMoney.setText(Html.fromHtml("总金额<font color = red>" + Language.POPLOTTERYTIP_RMB + f.format((long) zhushu * (long) beishu * 2) + "</font>" + ",投注后余额" + "<font color = red>"
				+ Language.POPLOTTERYTIP_RMB + com.esun.util.LotteryUtils.getLotRestBottom(UserInfo.getUserInfo().getUserBalance(), zhushu * beishu * 2) + "</font>"));
		tvJackpot.setText(Html.fromHtml("<font color = red>"
				+ Language.POPLOTTERYTIP_RMB
				+ getMaxMoney(SportteryBasketballLotteryInfo.getSportteryBasketballLotteryInfo().getActiveTeamCount(), SportteryBasketballLotteryInfo.getSportteryBasketballLotteryInfo()
						.getLotterList(), listSGType) + "</font>"));
		for (int i = 0; i < SportteryBasketballLotteryInfo.getSportteryBasketballLotteryInfo().getLotterList().size(); i++) {
			if ((Boolean) SportteryBasketballLotteryInfo.getSportteryBasketballLotteryInfo().getLotterList().get(i).get(IS_ACTIVE)) {
				tvJiezhiTime.setText(((SportteryBasketballAgainstInfo) SportteryBasketballLotteryInfo.getSportteryBasketballLotteryInfo().getLotterList().get(i).get(INFO)).getPtggEndTime());
				break;
			}
		}
		// tvBalance.setText(Language.POPLOTTERYTIP_RMB + com.esun.util.LotteryUtils.getLotRestBottom(UserInfo.getUserInfo().getUserBalance(), zhushu * beishu * 2));
	}

	/**
	 * @title: getZhushuCount
	 * @description:获取用户选择的比赛组数
	 * 
	 */
	private int getZhushuCount(int activeTeamCount, ArrayList<Hashtable<String, Object>> lotteryList, ArrayList<Hashtable<String, Object>> listSgType) {
		int zhushu_ = 0;

		if (activeTeamCount != 0) {
			int teamCount = activeTeamCount;
			int[][] pDetail;
			pDetail = new int[teamCount][2];
			int tCount = 0;
			for (int i = 0; i < lotteryList.size(); i++) {
				if ((Boolean) lotteryList.get(i).get(IS_ACTIVE)) {
					if ((Boolean) lotteryList.get(i).get(IS_DAN)) {
						pDetail[tCount][0] = 1;
					} else {
						pDetail[tCount][0] = 0;
					}
					int checkCount = 0;
					if ((Boolean) lotteryList.get(i).get(W)) {
						checkCount += 1;
					}
					if ((Boolean) lotteryList.get(i).get(L)) {
						checkCount += 1;
					}
					pDetail[tCount][1] = checkCount;
					tCount += 1;
				}
			}
			if (guoguanType == 3) {
				int[] pGGType;
				pGGType = new int[9];
				for (int i = 0; i < 9; i++) {
					pGGType[i] = 0;
				}
				for (int i = 2; i < listSgType.size() + 2; i++) {
					if ((Boolean) listSgType.get(i - 2).get(IS_CHECKED)) {
						pGGType[i] = 1;
						EDebug.o(SportteryBasketballBuyLottery.this, "checked item is " + i);
					}
				}
				zhushu_ = ESInterface.getZYTouZhuNum(teamCount, pDetail, pGGType, Integer.parseInt(Config.JINGCAILANQIU));
			} else if (guoguanType == 2) {
				int[] pGGType;
				pGGType = new int[2];
				for (int i = 0; i < listSgType.size(); i++) {
					if ((Boolean) listSgType.get(i).get(IS_CHECKED)) {
						pGGType[0] = (Integer) listSgType.get(i).get(TEAM_COUNT);
						pGGType[1] = (Integer) listSgType.get(i).get(TYPE);
						EDebug.o(SportteryBasketballBuyLottery.this, "team count is " + pGGType[0] + " type is " + pGGType[1]);
					}
				}
				zhushu_ = ESInterface.getDCTouZhuNum(teamCount, pDetail, pGGType, Integer.parseInt(Config.JINGCAILANQIU));
			} else {
				zhushu_ = 0;
			}

		}

		return zhushu_;
	}

	/**
	 * @title：getMaxMoney
	 * @description：获取最大中奖金额
	 */
	private String getMaxMoney(int activeTeamCount, ArrayList<Hashtable<String, Object>> lotteryList, ArrayList<Hashtable<String, Object>> listSgType) {
		EDebug.o(SportteryBasketballBuyLottery.this, " max money 走你");
		Object[] objTmp = { new TouZhuData(), new TouZhuData(), new TouZhuData(), new TouZhuData(), new TouZhuData(), new TouZhuData(), new TouZhuData(), new TouZhuData(), new TouZhuData(),
				new TouZhuData(), new TouZhuData(), new TouZhuData(), new TouZhuData(), new TouZhuData(), new TouZhuData(), };

		ArrayList<Hashtable<String, Object>> listActive = new ArrayList<Hashtable<String, Object>>();
		for (int i = 0; i < lotteryList.size(); i++) {
			if ((Boolean) lotteryList.get(i).get(IS_ACTIVE)) {
				listActive.add(lotteryList.get(i));
			}
		}
		EDebug.o(SportteryBasketballBuyLottery.this, " max money 走着");
		for (int i = 0; i < 15; i++) {
			TouZhuData tzd = (TouZhuData) objTmp[i];
			tzd.drawPL = 0;
			if (i < listActive.size()) {
				tzd.isEff = 1;
				if ((Boolean) listActive.get(i).get(IS_DAN)) {
					tzd.isDan = 1;
				} else {
					tzd.isDan = 0;
				}

				if ((Boolean) listActive.get(i).get(W)) {
					tzd.winPL = (float) Double.parseDouble(((SportteryBasketballAgainstInfo) listActive.get(i).get(INFO)).getWin());
				} else {
					tzd.winPL = 0;
				}
				// if ((Boolean) listActive.get(i).get(D)) {
				// tzd.drawPL = (float) Double.parseDouble(((SportteryBasketballAgainstInfo) listActive.get(i).get(INFO)).getDraw());
				// } else {
				// tzd.drawPL = 0;
				// }
				if ((Boolean) listActive.get(i).get(L)) {
					tzd.losePL = (float) Double.parseDouble(((SportteryBasketballAgainstInfo) listActive.get(i).get(INFO)).getLost());
				} else {
					tzd.losePL = 0;
				}
			} else {
				// tzd.drawPL = 0;
				tzd.isDan = 0;
				tzd.isEff = 0;
				tzd.losePL = 0;
				tzd.winPL = 0;
			}
		}
		double maxMoney = 0;
		if (activeTeamCount != 0) {
			if (guoguanType == 3) {
				EDebug.o(SportteryBasketballBuyLottery.this, " max money  get type is 3");
				int[] pGGType;
				pGGType = new int[9];
				for (int i = 0; i < 9; i++) {
					pGGType[i] = 0;
				}
				for (int i = 2; i < listSgType.size() + 2; i++) {
					if ((Boolean) listSgType.get(i - 2).get(IS_CHECKED)) {
						pGGType[i] = 1;
						EDebug.o(SportteryBasketballBuyLottery.this, "checked item is " + i);
					}
				}
				EDebug.o(SportteryBasketballBuyLottery.this, " max money 调用");
				EDebug.o(this, objTmp.toString() + pGGType.toString() + beishu + activeTeamCount);
				EDebug.o(this, pGGType.toString() + "===" + beishu + "===" + activeTeamCount);
				maxMoney = ESInterface.getZYGGMaxMoney(objTmp, pGGType, beishu, activeTeamCount, Integer.parseInt(Config.JINGCAILANQIU));
				EDebug.o(SportteryBasketballBuyLottery.this, " max money 完成");
			} else if (guoguanType == 2) {
				EDebug.o(SportteryBasketballBuyLottery.this, " max money  get type is 2");
				int[] pGGType;
				pGGType = new int[2];
				for (int i = 0; i < listSgType.size(); i++) {
					if ((Boolean) listSgType.get(i).get(IS_CHECKED)) {
						pGGType[0] = (Integer) listSgType.get(i).get(TEAM_COUNT);
						pGGType[1] = (Integer) listSgType.get(i).get(TYPE);
						EDebug.o(SportteryBasketballBuyLottery.this, "team count is " + pGGType[0] + " type is " + pGGType[1]);
					}
				}
				EDebug.o(SportteryBasketballBuyLottery.this, " max money 调用");
				maxMoney = ESInterface.getDCGGMaxMoney(objTmp, pGGType, beishu, activeTeamCount, Integer.parseInt(Config.JINGCAILANQIU));
				EDebug.o(SportteryBasketballBuyLottery.this, " max money 完成");
			} else {
				// zhushu_ = 0;
			}

		}
		DecimalFormat f = new DecimalFormat("0.00");
		EDebug.o(SportteryBasketballBuyLottery.this, " max money is" + maxMoney);
		return f.format(maxMoney);
	}

	/**
	 * @title：cleanTheList
	 * @description：清除用户对比赛选择列表的操作
	 */
	private void cleanTheList() {
		linearList.removeAllViews();
		for (int i = 0; i < SportteryBasketballLotteryInfo.getSportteryBasketballLotteryInfo().getLotterList().size(); i++) {
			if ((Boolean) SportteryBasketballLotteryInfo.getSportteryBasketballLotteryInfo().getLotterList().get(i).get(IS_ACTIVE)) {
				SportteryBasketballLotteryInfo.getSportteryBasketballLotteryInfo().getLotterList().get(i).put(IS_ACTIVE, false);
				SportteryBasketballLotteryInfo.getSportteryBasketballLotteryInfo().getLotterList().get(i).put(W, false);
				SportteryBasketballLotteryInfo.getSportteryBasketballLotteryInfo().getLotterList().get(i).put(L, false);
				SportteryBasketballLotteryInfo.getSportteryBasketballLotteryInfo().getLotterList().get(i).put(IS_DAN, false);

			}
		}
	}

	/**
	 * @title：deleteLotteryListItem
	 * @description：删除第position个队伍
	 */
	private void deleteLotteryListItem(int position) {
		SportteryBasketballLotteryInfo.getSportteryBasketballLotteryInfo().getLotterList().get(position).put(W, false);
		SportteryBasketballLotteryInfo.getSportteryBasketballLotteryInfo().getLotterList().get(position).put(L, false);
		SportteryBasketballLotteryInfo.getSportteryBasketballLotteryInfo().getLotterList().get(position).put(IS_ACTIVE, false);
		SportteryBasketballLotteryInfo.getSportteryBasketballLotteryInfo().getLotterList().get(position).put(IS_DAN, false);
	}

	/**
	 * @title updateLotteryListItem
	 * @description 更新第position个投注队伍
	 */
	private void updateLotteryListItem(int position, Boolean w, Boolean l, CheckBox cbxDan) {
		SportteryBasketballLotteryInfo.getSportteryBasketballLotteryInfo().getLotterList().get(position).put(IS_ACTIVE, true);
		SportteryBasketballLotteryInfo.getSportteryBasketballLotteryInfo().getLotterList().get(position).put(W, w);
		SportteryBasketballLotteryInfo.getSportteryBasketballLotteryInfo().getLotterList().get(position).put(L, l);
		cbxDan.setEnabled(true);
		if (!w && !l) {
			SportteryBasketballLotteryInfo.getSportteryBasketballLotteryInfo().getLotterList().get(position).put(IS_ACTIVE, false);
			cbxDan.setEnabled(false);
			cbxDan.setChecked(false);
			SportteryBasketballLotteryInfo.getSportteryBasketballLotteryInfo().getLotterList().get(position).put(IS_DAN, false);
			updateGuoGuanType();
		}
		EDebug.o(this, SportteryBasketballLotteryInfo.getSportteryBasketballLotteryInfo().getActiveTeamCount());
		EDebug.o(SportteryBasketballBuyLottery.this, "the active item count is " + SportteryBasketballLotteryInfo.getSportteryBasketballLotteryInfo().getActiveTeamCount());
		updateTextViewSet();
	}

	/**
	 * @title cleanDanItem
	 * @description 清除胆码
	 */
	private void cleanDanItem() {
		for (int i = 0; i < SportteryBasketballLotteryInfo.getSportteryBasketballLotteryInfo().getLotterList().size(); i++) {
			if ((Boolean) SportteryBasketballLotteryInfo.getSportteryBasketballLotteryInfo().getLotterList().get(i).get(IS_DAN)) {
				SportteryBasketballLotteryInfo.getSportteryBasketballLotteryInfo().getLotterList().get(i).put(IS_DAN, false);
			}
		}
		for (int i = 0; i < listCbxDan.size(); i++) {
			((CheckBox) listCbxDan.get(i)).setChecked(false);
		}
		updateTextViewSet();
	}

	/**
	 * @title getActiveItemNum
	 * @description 获取可用队伍个数
	 */
	public int getActiveItemNum() {
		int count = 0;
		for (int i = 0; i < SportteryBasketballLotteryInfo.getSportteryBasketballLotteryInfo().getLotterList().size(); i++) {
			if ((Boolean) SportteryBasketballLotteryInfo.getSportteryBasketballLotteryInfo().getLotterList().get(i).get(IS_ACTIVE)) {
				count++;
			}
		}
		return count;
	}

	/**
	 * @title getDanItemNum
	 * @description 获取已选中胆码数量
	 * */
	private int getDanItemNum() {
		int count = 0;
		for (int i = 0; i < SportteryBasketballLotteryInfo.getSportteryBasketballLotteryInfo().getLotterList().size(); i++) {
			if ((Boolean) SportteryBasketballLotteryInfo.getSportteryBasketballLotteryInfo().getLotterList().get(i).get(IS_DAN)) {
				count += 1;
			}
		}
		return count;
	}

	/**
	 * @title showDialogGuoguan
	 * @description 显示过关对话框
	 * */
	private void showDialogGuoguan() {
		if (dialogGuoguan == null) {
			dialogGuoguan = new Dialog(context, R.style.dialog);
		}
		dialogGuoguan.getWindow().setContentView(R.layout.sportterybasketball_dialog_guoguan_type);

		limitDanForDialogGuoguan = getDanItemNum();
		// 自由过关
		gridViewZiyou = (GridViewNoScroll) dialogGuoguan.getWindow().findViewById(R.id.sportterybasketballdialogguoguantype_grid_ziyou);
		// 多串过关
		gridViewDuochuan = (GridViewNoScroll) dialogGuoguan.getWindow().findViewById(R.id.sportterybasketballdialogguoguantype_grid_duochuan);
		rbZiyou = (RadioButton) dialogGuoguan.getWindow().findViewById(R.id.sportterybasketballdialogguoguantype_rb_ziyou);
		ZiyouListAdapter ziyouListAdapter = new ZiyouListAdapter(context);
		gridViewZiyou.setAdapter(ziyouListAdapter);
		duochuanListAdapter = new DuochuanListAdapter(listDuochuan);
		gridViewDuochuan.setAdapter(duochuanListAdapter);
		gridViewDuochuan.setVisibility(View.GONE);

		RadioGroup radioGroupTitle = (RadioGroup) dialogGuoguan.getWindow().findViewById(R.id.sportterybasketballdialogguoguantype_radiogroup_main);
		radioGroupTitle.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch (checkedId) {
				case R.id.sportterybasketballdialogguoguantype_rb_ziyou: // 选择自由过关页面
					gridViewDuochuan.setVisibility(View.GONE);
					gridViewZiyou.setVisibility(View.VISIBLE);
					break;
				case R.id.sportterybasketballdialogguoguantype_rb_duochuan:// 选择多串过关页面
					gridViewDuochuan.setVisibility(View.VISIBLE);
					gridViewZiyou.setVisibility(View.GONE);

					break;

				default:
					break;
				}
			}
		});

		OnClickListener onClickListener = new OnClickListener() {

			@Override
			public void onClick(View v) {
				switch (v.getId()) {
				case R.id.sportterybasketballdialogguoguantype_btn_submit:
					guoguanSubmit();
					break;

				case R.id.sportterybasketballdialogguoguantype_btn_cancel:
					dialogGuoguan.dismiss();
					break;

				default:
					break;
				}
			}
		};
		Button btnSubmit = (Button) dialogGuoguan.getWindow().findViewById(R.id.sportterybasketballdialogguoguantype_btn_submit);
		Button btnCancel = (Button) dialogGuoguan.getWindow().findViewById(R.id.sportterybasketballdialogguoguantype_btn_cancel);
		btnCancel.setOnClickListener(onClickListener);
		btnSubmit.setOnClickListener(onClickListener);
		dialogGuoguan.show();
	}

	/**
	 * @title guoguanSubmit
	 * @description 过关方式提交
	 */
	private void guoguanSubmit() {
		if (rbZiyou.isChecked()) {
			updateZiyouLimitDanNum();
		} else {
			updateDuochuanLimitDanNum();
		}
		EDebug.o(SportteryBasketballBuyLottery.this, "limit dan num is " + getLimitDanNum());

	}

	/**
	 * @title updateDuochuanLimitDanNum
	 * @description 更新多穿过关胆码上限
	 */
	private void updateDuochuanLimitDanNum() {
		int limitDan = 8;
		boolean b = false;
		StringBuffer sb = new StringBuffer();
		StringBuffer sbSgType = new StringBuffer();
		for (int i = 0; i < listDuochuan.size(); i++) {
			if ((Boolean) listDuochuan.get(i).get(IS_CHECKED)) {
				b = true;
				limitDan = (Integer) listDuochuan.get(i).get(TEAM_COUNT);
				sb.append((String) listDuochuan.get(i).get(STR_TYPE));
				sbSgType.append(Integer.toString((Integer) listDuochuan.get(i).get(TAG)));
			}
		}
		if (b) {
			dialogGuoguan.dismiss();
			if (limitDanForDialogGuoguan == 0) {
				cleanDanItem();
				setListCbxDan(false);
			}
			setLimitDanNum(limitDan - 1);
			btnGuoguan.setText("多串过关");
			strGuoguan = sb.toString();
			tvGuoguan.setText(strGuoguan);
			guoguanType = 2;
			sgtype = sbSgType.toString();
			EDebug.o(SportteryBasketballBuyLottery.this, "guoguanType is " + guoguanType + " && sgtype is [" + sgtype + "]");
			listSGType.clear();
			listSGType.addAll(listDuochuan);
			updateTextViewSet();
		} else {
			Toast.makeText(context, "请选择", Toast.LENGTH_SHORT).show();
		}
	}

	/**
	 * @title updateZiyouLimitDanNum
	 * @description 更新自由过关胆码上限
	 */
	private void updateZiyouLimitDanNum() {
		int limitDan = 8;
		int count = 0;
		StringBuffer sb = new StringBuffer();
		StringBuffer sbSgType = new StringBuffer();
		for (int i = 0; i < listZiyou.size(); i++) {
			if ((Boolean) listZiyou.get(i).get(IS_CHECKED)) {
				count += 1;
				sb.append((String) listZiyou.get(i).get(STR_TYPE)).append(Config.ONESPACE);
				sbSgType.append(Integer.toString((Integer) listZiyou.get(i).get(TAG))).append(Config.COMMA);

				if ((Integer) listZiyou.get(i).get(TEAM_COUNT) < limitDan) {
					limitDan = (Integer) listZiyou.get(i).get(TEAM_COUNT);
				}
			}
		}
		if (count == 0) {
			Toast.makeText(context, "请选择", Toast.LENGTH_SHORT).show();
		} else {
			if (limitDanForDialogGuoguan == 0) {
				cleanDanItem();
			}
			listSGType.clear();
			listSGType.addAll(listZiyou);
			if (limitDan == getActiveItemNum()) {
				setLimitDanNum(0);
				setListCbxDan(false);
			} else {
				setListCbxDan(true);
				setLimitDanNum(limitDan - 1);
			}
			dialogGuoguan.dismiss();
			btnGuoguan.setText("自由过关");
			strGuoguan = sb.toString();
			tvGuoguan.setText(strGuoguan);
			guoguanType = 3;
			sgtype = sbSgType.toString();
			sgtype = sgtype.substring(0, sgtype.length() - 1);
			updateTextViewSet();
			EDebug.o(SportteryBasketballBuyLottery.this, "guoguanType is " + guoguanType + " && sgtype is [" + sgtype + "]");
		}
	}

	/**
	 * @title updateGuoguanTypeList
	 * @description 更新过关方式列表
	 */
	private void updateGuoguanTypeList() {
		listDuochuan.clear();
		listZiyou.clear();

		if (getActiveItemNum() > 1) {
			listZiyou.add(addItemTable(1, 2, 1));
			if (getActiveItemNum() > 2) {
				listZiyou.add(addItemTable(3, 3, 1));
				listDuochuan.add(addItemTable(56, 3, 3));
				listDuochuan.add(addItemTable(4, 3, 4));
				if (getActiveItemNum() > 3) {
					listZiyou.add(addItemTable(7, 4, 1));
					listDuochuan.add(addItemTable(8, 4, 4));
					listDuochuan.add(addItemTable(9, 4, 5));
					listDuochuan.add(addItemTable(10, 4, 6));
					listDuochuan.add(addItemTable(12, 4, 11));
					if (getActiveItemNum() > 4) {
						listZiyou.add(addItemTable(15, 5, 1));
						listDuochuan.add(addItemTable(16, 5, 5));
						listDuochuan.add(addItemTable(17, 5, 6));
						listDuochuan.add(addItemTable(18, 5, 10));
						listDuochuan.add(addItemTable(20, 5, 16));
						listDuochuan.add(addItemTable(21, 5, 20));
						listDuochuan.add(addItemTable(23, 5, 26));
						if (getActiveItemNum() > 5) {
							listZiyou.add(addItemTable(26, 6, 1));
							listDuochuan.add(addItemTable(27, 6, 6));
							listDuochuan.add(addItemTable(28, 6, 7));
							listDuochuan.add(addItemTable(29, 6, 15));
							listDuochuan.add(addItemTable(30, 6, 20));
							listDuochuan.add(addItemTable(57, 6, 22));
							listDuochuan.add(addItemTable(32, 6, 35));
							listDuochuan.add(addItemTable(34, 6, 42));
							listDuochuan.add(addItemTable(35, 6, 50));
							listDuochuan.add(addItemTable(37, 6, 57));
							if (getActiveItemNum() > 6) {
								listZiyou.add(addItemTable(41, 7, 1));
								listDuochuan.add(addItemTable(42, 7, 7));
								listDuochuan.add(addItemTable(43, 7, 8));
								listDuochuan.add(addItemTable(44, 7, 21));
								listDuochuan.add(addItemTable(45, 7, 35));
								listDuochuan.add(addItemTable(46, 7, 120));
								if (getActiveItemNum() > 7) {
									listZiyou.add(addItemTable(48, 8, 1));
									listDuochuan.add(addItemTable(49, 8, 8));
									listDuochuan.add(addItemTable(50, 8, 9));
									listDuochuan.add(addItemTable(51, 8, 28));
									listDuochuan.add(addItemTable(52, 8, 56));
									listDuochuan.add(addItemTable(53, 8, 70));
									listDuochuan.add(addItemTable(54, 8, 247));

								}
							}
						}

					}
				}
			}
		} else {
			listDuochuan.clear();
			listZiyou.clear();
		}
	}

	/**
	 * @title Hashtable
	 * @description 添加一项过关选项
	 * @param tag
	 *            过关选项标记
	 * @param teamCount
	 *            组数
	 * @param type
	 *            过关类型
	 */
	private Hashtable<String, Object> addItemTable(int tag, int teamCount, int type) {
		Hashtable<String, Object> table = new Hashtable<String, Object>();
		table.put(TAG, tag);
		table.put(TEAM_COUNT, teamCount);
		table.put(TYPE, type);
		String strType = Integer.toString(teamCount) + "串" + Integer.toString(type);
		table.put(STR_TYPE, strType);
		table.put(IS_CHECKED, false);
		return table;
	}

	/**
	 * @className ZiyouListAdapter
	 * @description 对话框中自由过关GridView的适配器
	 * @author liugan
	 */
	class ZiyouListAdapter extends BaseAdapter {
		private LayoutInflater listInflater;

		public ZiyouListAdapter(Context context) {
			this.listInflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			return listZiyou.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {

			if (convertView == null) {
				convertView = (CheckBox) listInflater.inflate(R.layout.sportterybasketball_dialog_guoguan_ziyou_item, null);
			}
			final CheckBox cbxItem = (CheckBox) convertView.findViewById(R.id.sportterybasketballdialogguoguanziyouitem_cbx_item);
			cbxItem.setText((String) listZiyou.get(position).get(STR_TYPE));
			OnCheckedChangeListener onCheckedChangeListener = new OnCheckedChangeListener() {

				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					if (!((Integer) listZiyou.get(position).get(TEAM_COUNT) > limitDanForDialogGuoguan)) {
						cbxItem.setChecked(false);
					} else {
						listZiyou.get(position).put(IS_CHECKED, isChecked);
						if (getActiveItemNum() < 9 && (Integer) listZiyou.get(position).get(TEAM_COUNT) == getActiveItemNum()) {
							limitDanForDialogGuoguan = 0;
						}
					}

				}
			};
			cbxItem.setOnCheckedChangeListener(onCheckedChangeListener);

			return convertView;
		}
	}

	/**
	 * @className DuochuanListAdapter
	 * @description 对话框中多串过关GridView的适配器
	 * @author liugan
	 */
	class DuochuanListAdapter extends BaseAdapter {

		ArrayList<Hashtable<String, Object>> list = new ArrayList<Hashtable<String, Object>>();

		public DuochuanListAdapter(ArrayList<Hashtable<String, Object>> dataList) {
			this.list = dataList;
		}

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Hashtable<String, Object> getItem(int position) {
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			final int index = position;

			if (convertView == null) {
				convertView = (CheckBox) LayoutInflater.from(context).inflate(R.layout.sportteryfootball_dialog_guoguan_duochuan_item, null);
			}
			final CheckBox cbxDuochuan = (CheckBox) convertView.findViewById(R.id.sportteryfootballdialogguoguanduochuanitem_rb_item);
			cbxDuochuan.setChecked((Boolean) list.get(position).get(IS_CHECKED));
			cbxDuochuan.setText((String) list.get(position).get(STR_TYPE));

			cbxDuochuan.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// 判断点击的是不是自己
					if (lastItem != index) {
						// 判断之前是否有被选中的按钮
						if (lastItem >= 0) {
							// 将所有按钮重置
							for (int i = 0; i < duochuanListAdapter.getCount(); i++) {
								Hashtable<String, Object> each = (Hashtable<String, Object>) duochuanListAdapter.getItem(i);
								each.put(IS_CHECKED, false);
								EDebug.o("把第" + i + "个改成不选中状态");
							}
						}
						// 将被选中按钮标记并置为选中
						lastItem = index;
						Hashtable<String, Object> each = (Hashtable<String, Object>) duochuanListAdapter.getItem(lastItem);
						each.put(IS_CHECKED, true);
						EDebug.o("把第" + lastItem + "个改成选中状态");
					} else {
						// 如果点击的是自己，则将自己置为选中状态
						Hashtable<String, Object> each = (Hashtable<String, Object>) duochuanListAdapter.getItem(index);
						each.put(IS_CHECKED, true);
						EDebug.o("点击的自己");
					}
					duochuanListAdapter.notifyDataSetChanged();
				}
			});

			return convertView;
		}

	}

	/**
	 * @title
	 * @description 购买成功后的操作，弹出对话框
	 */
	private void buySucceed() {
		dialogBuySucceed = new Dialog(context, R.style.dialog);
		dialogBuySucceed.setContentView(R.layout.viewerutil_dialog_showbuysucceed);
		((TextView) dialogBuySucceed.getWindow().findViewById(R.id.viewerutildialogshowbuysucceed_tv_msg)).setText("让球胜平负复式代购成功");

		DecimalFormat f = new DecimalFormat("0.00");
		((TextView) dialogBuySucceed.getWindow().findViewById(R.id.viewerutildialogshowbuysucceed_tv_term)).setText("注数:" + Integer.toString(zhushu));
		((TextView) dialogBuySucceed.getWindow().findViewById(R.id.viewerutildialogshowbuysucceed_tv_count)).setText("倍投:" + Integer.toString(beishu));

		((TextView) dialogBuySucceed.getWindow().findViewById(R.id.viewerutildialogshowbuysucceed_tv_cost)).setText("消费:" + Language.POPLOTTERYTIP_RMB + f.format((long) zhushu * (long) beishu * 2));
		((TextView) dialogBuySucceed.getWindow().findViewById(R.id.viewerutildialogshowbuysucceed_tv_balance)).setText("剩余金额:" + Language.POPLOTTERYTIP_RMB + UserInfo.getUserInfo().getUserBalance());

		OnClickListener dialogSucceedListener = new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				SportteryBasketballLotteryInfo.getSportteryBasketballLotteryInfo().getLotterList().clear();

				switch (v.getId()) {
				case R.id.viewerutildialogshowbuysucceed_btn_back:
					dialogBuySucceed.dismiss();
					Message msg = SportteryBasketballChooseList.handlerChoosePage.obtainMessage(SportteryBasketballChooseList.DO_REFRESH, null);
					SportteryBasketballChooseList.handlerChoosePage.sendMessage(msg);
					((Activity) context).setContentView(chooseListPage.getView());
					break;

				case R.id.viewerutildialogshowbuysucceed_btn_toHall:
					dialogBuySucceed.dismiss();
					SportteryBasketballChooseAct.pageFlag = 0;
					((Activity) context).getParent().finish();
					break;

				default:
					break;
				}

			}
		};
		((Button) dialogBuySucceed.getWindow().findViewById(R.id.viewerutildialogshowbuysucceed_btn_back)).setOnClickListener(dialogSucceedListener);
		((Button) dialogBuySucceed.getWindow().findViewById(R.id.viewerutildialogshowbuysucceed_btn_toHall)).setOnClickListener(dialogSucceedListener);

		dialogBuySucceed.show();
	}

	private View.OnClickListener moneyNotEnoughListener;

	/**
	 * @title popBalanceNotEnoughAlert
	 * @description 余额不足提示
	 */
	private void popBalanceNotEnoughAlert() {
		if (dialogBalanceNotEnough == null) {
			dialogBalanceNotEnough = new Dialog(context, R.style.dialog);
		}

		if (moneyNotEnoughListener == null) {
			moneyNotEnoughListener = new OnClickListener() {

				@Override
				public void onClick(View v) {
					switch (v.getId()) {
					case R.id.shiyixuanwu_alert_money_not_enough_btn_cancel:
						break;

					case R.id.shiyixuanwu_alert_money_not_enough_btn_recharge:// 跳转到充值界面
						UserInfo.getUserInfo().setBuyingRecharge(true);

						((Activity) context).startActivity(new Intent().setClass(context, RechargeMenuPage.class));

						break;
					}

					dialogBalanceNotEnough.dismiss();
				}
			};
		}

		View view = ((Activity) context).getLayoutInflater().inflate(R.layout.shiyixuanwu_alert_money_not_enough, null);

		((Button) view.findViewById(R.id.shiyixuanwu_alert_money_not_enough_btn_cancel)).setOnClickListener(moneyNotEnoughListener);
		((Button) view.findViewById(R.id.shiyixuanwu_alert_money_not_enough_btn_recharge)).setOnClickListener(moneyNotEnoughListener);

		dialogBalanceNotEnough.setContentView(view);
		dialogBalanceNotEnough.show();
	}

	@Override
	public void setSportteryChooseListPage(SportteryChooseList chooseListPage) {
		this.chooseListPage = chooseListPage;
	}

}
